<?php

//phpinfo();return;
set_include_path('../include');
//error_reporting(E_ALL);
require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

// setup the application logger
$logger = new Zend_Log(new Zend_Log_Writer_Null());

try {
// загрузка конфигурации приложения
    $config = new Zend_Config_Ini('../settings.ini', 'development');
    Zend_Registry::set('config', $config);

// создание объекта для системного журнала
    $logger->addWriter(new Zend_Log_Writer_Stream($config->logging->file));
    Zend_Registry::set('logger', $logger);

// соединение с БД
    $params = array(
        'host' => $config->database->hostname,
        'username' => $config->database->username,
        'password' => $config->database->password,
        'dbname' => $config->database->database
    );

    $db = Zend_Db::factory($config->database->type, $params);
    Zend_Registry::set('db', $db);
    $db->query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'");

// настройка аутентификации
    $auth = Zend_Auth::getInstance();
    $auth->setStorage(new Zend_Auth_Storage_Session());

// обработка запроса пользователя
    $controller = Zend_Controller_Front::getInstance();
    $controller->setControllerDirectory(
            $config->paths->base . '/include/Controllers');

    $controller->registerPlugin(new CustomControllerAclManager($auth));

// настройки визуализатора
    $vr = new Zend_Controller_Action_Helper_ViewRenderer();
    $vr->setView(new Templater());

    $vr->setViewSuffix('html');

    Zend_Controller_Action_HelperBroker::addHelper($vr);

// далее будем генерировать динамические роуты

    /*     * *******************************************************
     * роут для просмотра тендера (/tender/id_тендера)
     * ****************************************************** */
    $route = new Zend_Controller_Router_Route(
            'tender/:id/*', array('controller' => 'tenders',
        'action' => 'viewtender')
    );

    $controller->getRouter()->addRoute('viewtender', $route);

    $route = new Zend_Controller_Router_Route(
            'tender/:id/*', array('controller' => 'tenders',
        'action' => 'viewtender')
    );

    $controller->getRouter()->addRoute('viewtender', $route);

    /*     * *********************************************************
     * роут для подачи заявки на тендер (tender/id_тендера/gettrying)
     * ******************************************************** */
    $route = new Zend_Controller_Router_Route(
            'tender/:id/try/*', array('controller' => 'tenders',
        'action' => 'gettrying')
    );

    $controller->getRouter()->addRoute('trytender', $route);

    $route = new Zend_Controller_Router_Route(
            'index/:page/*', array('controller' => 'index',
        'action' => 'index')
    );

    $controller->getRouter()->addRoute('pagetender', $route);

    $route = new Zend_Controller_Router_Route(
            'index/archive/*', array('controller' => 'index',
        'action' => 'archive')
    );

    $controller->getRouter()->addRoute('indexarchivetender', $route);

    $route = new Zend_Controller_Router_Route(
            'index/archive/:page/*', array('controller' => 'index',
        'action' => 'archive')
    );

    $controller->getRouter()->addRoute('pagearchivetender', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/tenders/new/*', array('controller' => 'admin',
        'action' => 'addtender')
    );

    $controller->getRouter()->addRoute('addtender', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/tenders/edit/:id/*', array('controller' => 'admin',
        'action' => 'edittender')
    );

    $controller->getRouter()->addRoute('edittender', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/tenders/archive/:id/*', array('controller' => 'admin',
        'action' => 'archivetender')
    );

    $controller->getRouter()->addRoute('archivetender', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/tenders/delete/:id/*', array('controller' => 'admin',
        'action' => 'deletetender')
    );

    $controller->getRouter()->addRoute('deletetender', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/tenders/users/:id/*', array('controller' => 'admin',
        'action' => 'tendermembers')
    );

    $controller->getRouter()->addRoute('tendermembers', $route);


//********** Begin route categories *************//

    $route = new Zend_Controller_Router_Route(
            'admin/categories/edit/:id/*', array('controller' => 'admin',
        'action' => 'editcategory')
    );

    $controller->getRouter()->addRoute('editcategory', $route);

    $route = new Zend_Controller_Router_Route(
            'category/:id/*', array('controller' => 'categories',
        'action' => 'viewcategory')
    );

    $controller->getRouter()->addRoute('viewcategory', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/categories/delete/:id/*', array('controller' => 'admin',
        'action' => 'deletecategory')
    );

    $controller->getRouter()->addRoute('deletecategory', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/categories/add/*', array('controller' => 'admin',
        'action' => 'addcategory')
    );

    $controller->getRouter()->addRoute('addcategory', $route);

//********** End route categories *************//
//********** Begin route projects *************//

    /* $route = new Zend_Controller_Router_Route(
      'viewproject/:id/*', array('controller' => 'projects',
      'action' => 'viewproject')
      );

      $controller->getRouter()->addRoute('viewproject', $route); */

    $route = new Zend_Controller_Router_Route(
            'admin/projects/edit/:id/*', array('controller' => 'admin',
        'action' => 'editproject')
    );

    $controller->getRouter()->addRoute('editproject', $route);

    $route = new Zend_Controller_Router_Route(
            'project/:id/*', array('controller' => 'projects',
        'action' => 'viewproject')
    );

    $controller->getRouter()->addRoute('viewproject', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/projects/delete/:id/*', array('controller' => 'admin',
        'action' => 'deleteproject')
    );

    $controller->getRouter()->addRoute('deleteproject', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/projects/add/*', array('controller' => 'admin',
        'action' => 'addproject')
    );

    $controller->getRouter()->addRoute('addproject', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/meta-tags/tenders/*', array('controller' => 'seo',
        'action' => 'metatenders')
    );

    $controller->getRouter()->addRoute('metatenders', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/meta-tags/tenders/edit/:id/*', array('controller' => 'seo',
        'action' => 'edittender')
    );

    $controller->getRouter()->addRoute('seoedittender', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/meta-tags/tenders/edit/complete/*', array('controller' => 'seo',
        'action' => 'edittendercomplete')
    );

    $controller->getRouter()->addRoute('seoedittendercomplete', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/meta-tags/index/*', array('controller' => 'seo',
        'action' => 'metaindex')
    );

    $controller->getRouter()->addRoute('metaindex', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/meta-tags/index/complete/*', array('controller' => 'seo',
        'action' => 'metaindexcomplete')
    );

    $controller->getRouter()->addRoute('metaindexcomplete', $route);

//********** End route progects *************//

    $route = new Zend_Controller_Router_Route(
            'tender/download/:id/*', array('controller' => 'utility',
        'action' => 'download')
    );

    $controller->getRouter()->addRoute('download', $route);

    $route = new Zend_Controller_Router_Route(
            'tender/download/all/:id/*', array('controller' => 'utility',
        'action' => 'downloadall')
    );

    $controller->getRouter()->addRoute('downloadall', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/pages/add/*', array('controller' => 'admin',
        'action' => 'addpage')
    );

    $controller->getRouter()->addRoute('addpage', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/pages/add/complete/*', array('controller' => 'admin',
        'action' => 'addpagecomplete')
    );

    $controller->getRouter()->addRoute('addpagecomplete', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/pages/add/*', array('controller' => 'seo',
        'action' => 'addpage')
    );

    $controller->getRouter()->addRoute('seoaddpage', $route);

    $route = new Zend_Controller_Router_Route(
            'seo/pages/add/complete/*', array('controller' => 'seo',
        'action' => 'addpagecomplete')
    );

    $controller->getRouter()->addRoute('seoaddpagecomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/pages/edit/:id/*', array('controller' => 'admin',
        'action' => 'editpage')
    );

    $controller->getRouter()->addRoute('editpage', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/pages/edit/:id/*', array('controller' => 'seo',
        'action' => 'editpage')
    );

    $controller->getRouter()->addRoute('seoeditpage', $route);
    
    $route = new Zend_Controller_Router_Route(
            'seo/pages/edit/complete/*', array('controller' => 'seo',
        'action' => 'editpagecomplete')
    );

    $controller->getRouter()->addRoute('seoeditpagecomplete', $route);

    $route = new Zend_Controller_Router_Route(
            '/pages/:alias/*', array('controller' => 'index',
        'action' => 'viewpage')
    );

    $controller->getRouter()->addRoute('viewpage', $route);

    $route = new Zend_Controller_Router_Route(
            'request/downloadrequest/:id/*', array('controller' => 'utility',
        'action' => 'downloadrequest')
    );

    $controller->getRouter()->addRoute('downloadrequest', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/users/edit/:id/*', array('controller' => 'admin',
        'action' => 'edituser')
    );

    $controller->getRouter()->addRoute('edituser', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/users/delete/:id/*', array('controller' => 'admin',
        'action' => 'deleteuser')
    );

    $controller->getRouter()->addRoute('deleteuser', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/users/ban/:id/*', array('controller' => 'admin',
        'action' => 'banuser')
    );

    $controller->getRouter()->addRoute('banuser', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/users/approve/:id/*', array('controller' => 'admin',
        'action' => 'approveuser')
    );

    $controller->getRouter()->addRoute('approveuser', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/requests/:id/*', array('controller' => 'admin',
        'action' => 'viewrequest')
    );

    $controller->getRouter()->addRoute('viewrequest', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/requests/approve/:id/*', array('controller' => 'admin',
        'action' => 'approverequest')
    );

    $controller->getRouter()->addRoute('approverequest', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/requests/:id/setAsWinner/*', array('controller' => 'admin',
        'action' => 'finishtender')
    );

    $controller->getRouter()->addRoute('finishtender', $route);

    $route = new Zend_Controller_Router_Route(
            'account/viewrequest/:id/*', array('controller' => 'account',
        'action' => 'viewrequest')
    );

    $controller->getRouter()->addRoute('request', $route);

    $route = new Zend_Controller_Router_Route(
            'account/request/edit/:id/*', array('controller' => 'account',
        'action' => 'requestedit')
    );

    $controller->getRouter()->addRoute('requestedit', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/globalconfig/edit/:id/*', array('controller' => 'admin',
        'action' => 'editconfig')
    );

    $controller->getRouter()->addRoute('editconfig', $route);
    
    $route = new Zend_Controller_Router_Route(
            'admin/seoconfig/edit/:id/*', array('controller' => 'admin',
        'action' => 'editseoconfig')
    );

    $controller->getRouter()->addRoute('editseoconfig', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/seoconfig/delete/:id/*', array('controller' => 'admin',
        'action' => 'deleteseoconfig')
    );

    $controller->getRouter()->addRoute('deleteseoconfig', $route);
    
    $route = new Zend_Controller_Router_Route(
            'admin/globalconfig/delete/:id/*', array('controller' => 'admin',
        'action' => 'deleteconfig')
    );

    $controller->getRouter()->addRoute('deleteconfig', $route);

//-------------------------- роуты для complete-функций -----------------

    $route = new Zend_Controller_Router_Route(
            'account/request/edit/complete/*', array('controller' => 'account',
        'action' => 'editrequestcomplete')
    );

    $controller->getRouter()->addRoute('editrequestcomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/tenders/add/complete/*', array('controller' => 'admin',
        'action' => 'addtendercomplete')
    );

    $controller->getRouter()->addRoute('addtendercomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/tender/edit/complete/*', array('controller' => 'admin',
        'action' => 'edittendercomplete')
    );

    $controller->getRouter()->addRoute('edittendercomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/categories/add/complete/*', array('controller' => 'admin',
        'action' => 'addcategorycomplete')
    );

    $controller->getRouter()->addRoute('addcategorycomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/category/edit/complete/*', array('controller' => 'admin',
        'action' => 'editcategorycomplete')
    );

    $controller->getRouter()->addRoute('editcategorycomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/projects/add/complete/*', array('controller' => 'admin',
        'action' => 'addprojectcomplete')
    );

    $controller->getRouter()->addRoute('addprojectcomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/project/edit/complete/*', array('controller' => 'admin',
        'action' => 'editprojectcomplete')
    );

    $controller->getRouter()->addRoute('editprojectcomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/user/edit/complete/*', array('controller' => 'admin',
        'action' => 'editusercomplete')
    );

    $controller->getRouter()->addRoute('editusercomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'admin/page/edit/complete/*', array('controller' => 'admin',
        'action' => 'editpagecomplete')
    );

    $controller->getRouter()->addRoute('editpagecomplete', $route);

    $route = new Zend_Controller_Router_Route(
            'tender/sendrequest/complete/*', array('controller' => 'index',
        'action' => 'sendrequestcomplete')
    );

    $controller->getRouter()->addRoute('sendrequestcomplete', $route);

//-------------------------- роуты для complete-функций -----------------

    $controller->dispatch();
} 
catch (Exception $ex) {
    $logger->emerg($ex->getMessage());
    
    header('Location: /error.html');
    exit;
}
?>
